package com.maybach.workflow.core.expression;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * Created by houenxun on 15/8/18.
 * 逻辑表达式帮助类
 */
public class LogicalExprHelper {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    IExpression expression;

    public boolean judge(String expr, Map<String, Object> args){
        return expression.eval(Boolean.class, expr, args);
    }

    /**
     * 优雅地判断 如果expr是空串 返回true，如果抛出异常，返回false
     * @param expr
     * @param args
     * @return
     */
    public boolean judgeFairily(String expr, Map<String, Object> args){
        if(StringUtils.isBlank(expr)){
            return true;
        }
        try{
            return this.judge(expr, args);
        }catch (Exception e){
/*            if(ConfigHolder.instance.getPrintjudgeException()) {
                logger.error("expr=" + expr + ", agrs=" + args, e);
            }*/
            return false;
        }
    }

}
